package com.erhuogo.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.dao.ThreeCategoryDao;
import com.erhuogo.entity.ThreeCategory;
import com.erhuogo.enumerate.OpenStatus;
import com.erhuogo.service.ThreeCategoryService;
import com.erhuogo.util.DateUtils;

/**
 * 三级分类Service的实现类
 * @author aibinxiao
 * @date 2016年10月26日 下午12:04:56
 */
public class ThreeCategoryServiceImpl implements ThreeCategoryService {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ThreeCategoryServiceImpl.class);
				
	// 注入ThreeCategoryDao
	private ThreeCategoryDao threeCategoryDao;

	public ThreeCategoryDao getThreeCategoryDao() {
		return threeCategoryDao;
	}

	public void setThreeCategoryDao(ThreeCategoryDao threeCategoryDao) {
		this.threeCategoryDao = threeCategoryDao;
	}

	// 1.根据二级分类id获取所有的三级分类的Service实现类
	@Override
	public List<ThreeCategory> findThreeCateByTwoId(Integer twoCategoryId) {
		List<ThreeCategory> listThreeCategory = null;
		if (twoCategoryId != null){
			listThreeCategory = threeCategoryDao.findThreeCategoryByTwoId(twoCategoryId);
		}
		return listThreeCategory;
	}

	// 2.分页条件查询指定一级分类和二级分类下的三级分类Service实现类
	@Override
	public GridPageBean<ThreeCategory> findByPage(String pageNo, String pageSize, String id, String name, String code,
			String startDate, String endDate, String oneCategoryId, String twoCategoryId) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = threeCategoryDao.countByParams(id, name, code, startDate, endDate, oneCategoryId,twoCategoryId);
		GridPageBean<ThreeCategory> result = new GridPageBean<ThreeCategory>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<ThreeCategory> rows = threeCategoryDao.findPageByParams(id,name,code,startDate,endDate,oneCategoryId,twoCategoryId,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<ThreeCategory> rows = threeCategoryDao.findPageByParams(id,name,code,startDate,endDate,oneCategoryId,twoCategoryId,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	// 3.根据ID查询三级分类Service实现类
	@Override
	public ThreeCategory findById(String idStr) {
		ThreeCategory threeCategory = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			threeCategory = threeCategoryDao.findById(id);
		}
		return threeCategory;
	}

	// 4.保存或更新三级分类信息Service实现类
	@Override
	public void save(String idStr, String code, String name, String desc, String oneCategoryIdStr, String twoCategoryIdStr) throws Exception {
		ThreeCategory threeCategory = new ThreeCategory();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			threeCategory = threeCategoryDao.findById(id);
		}else{
			// id为空则是添加新的三级分类
			threeCategory.setCreateTime(DateUtils.DateToStr(new Date()));
			threeCategory.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(code)){
			threeCategory.setCode(code);
		}
		if(!StringUtils.isEmpty(name)){
			threeCategory.setName(name);
		}
		if(!StringUtils.isEmpty(desc)){
			threeCategory.setDesc(desc);
		}
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			threeCategory.setOneCategoryId(oneCategoryId);
		}
		if(!StringUtils.isEmpty(twoCategoryIdStr)){
			Integer twoCategoryId = Integer.valueOf(twoCategoryIdStr);
			threeCategory.setTwoCategoryId(twoCategoryId);
		}
		threeCategory.setAlterTime(DateUtils.DateToStr(new Date()));
		threeCategoryDao.saveThreeCategory(threeCategory);
	}

	// 5.更新三级分类信息的启用状态Service实现类
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception{
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			threeCategoryDao.updateStatus(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			threeCategoryDao.updateStatus(id, OpenStatus.SHIELD.getStatusCode());
		}
	}
	
}
